/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
var sname;
var pname;
var cname;
var aname;
var address;
var category;
$(function () {
    $(".sub_button").click(function () {
        var t = $(this).html();
        if ($.trim(t) === "下一步") {
            sname = $.trim($("#shopname").val());
            pname = $.trim($("#sel_pro").val());
            cname = $.trim($("#sel_city").val());
            aname = $.trim($("#sel_area").val());
            address = $.trim($("#shopaddress").val());
            category = $.trim($("#cate_par").val());
            if (sname === "") {
                alert("请填写商铺名称");
                return;
            }
            if (pname === "0" || pname === "") {
                alert("请选择省");
                return;
            }
            if (cname === "0" || cname === "") {
                alert("请选择市区");
                return;
            }
            if (aname === "0" || aname === "") {
                alert("请选择区域");
                return;
            }
            if (address === "") {
                alert("请填写详细地址");
                return;
            }
            if (category === 0) {
                alert("请选择商铺类别");
                return;
            }
            $(".sub_info_one").hide();
            $(".sub_info_two").hide();
            $(".sub_info_three").hide();
            $(".sub_info_two").show();
            $(".sub_img").find("img").attr("src", "/Manage/img/shop/sub_2.png");
            $(this).html("提交");
            $("#nextpage_shopname").html(sname);
            $("#nextpage_address").html(address);
        }
        if ($.trim(t) === "提交") {
            var tname = $.trim($("#truename").val());
            var idcard = $.trim($("#idnumber").val());
            var mobile = $.trim($("#mobile").val());
            var email = $.trim($("#email").val());
            var idimg = $.trim($("#hiddenID").val());
            var licenimg = $.trim($("#hiddenLicense").val());
            
            if (tname === "") {
                $("#error_name").html("<span class='glyphicon glyphicon-question-sign' style='color:#e41f4a;'></span>请填写真实姓名");
                return;
            }
            if (!isChinaName(tname)) {
                $("#error_name").html("<span class='glyphicon glyphicon-question-sign' style='color:#e41f4a;'></span>真实姓名必须是汉字");
                return;
            }
            if (idcard === "") {
                $("#error_idnumber").html("<span class='glyphicon glyphicon-question-sign' style='color:#e41f4a;'></span>请填写身份证号码");
                return;
            }
            if (!isCardNo(idcard)) {
                $("#error_idnumber").html("<span class='glyphicon glyphicon-question-sign' style='color:#e41f4a;'></span>身份证号码格式不正确");
                return;
            }
            if (mobile === "") {
                $("#error_mobile").html("<span class='glyphicon glyphicon-question-sign' style='color:#e41f4a;'></span>请填写手机号码");
                return;
            }
            if (!isMobile(mobile)) {
                $("#error_mobile").html("<span class='glyphicon glyphicon-question-sign' style='color:#e41f4a;'></span>手机号码格式不正确");
                return;
            }
            if (email === "") {
                $("#error_email").html("<span class='glyphicon glyphicon-question-sign' style='color:#e41f4a;'></span>请填写email");
                return;
            }
            if (!isEmail(email)) {
                $("#error_email").html("<span class='glyphicon glyphicon-question-sign' style='color:#e41f4a;'></span>email格式不正确");
                return;
            }
            if (idimg === "") {
                $("#error_id_img").html("<span class='glyphicon glyphicon-question-sign' style='color:#e41f4a;'></span>请上传身份证图片");
                return;
            }
            if (licenimg === "") {
                $("#error_license_img").html("<span class='glyphicon glyphicon-question-sign' style='color:#e41f4a;'></span>请上传营业执照图片");
                return;
            }
            $.ajax({
            url: '/Manage/addShop.do',
            type: 'POST',
            data: {shopName:sname,province:pname,city:cname,area:aname,address:address,categoryId:category, trueName:tname, identityCard:idcard, mobile:mobile, email:email, idImageUrl:idimg, licenseUrl:licenimg},
            success: function (res) {
                if (res.success) {
                    $(".sub_info_one").hide();
                    $(".sub_info_two").hide();
                    $(".sub_info_three").hide();
                    $(".sub_info_three").show();
                    $(".sub_img").find("img").attr("src", "/Manage/img/shop/sub_3.png");
                    $(this).html("返回首页");
                } else {
                    alert(res.message);
                }
            }
        });
        }
        
    });
    //上传身份证
    $("#upload_idnumber_button").click(function () {
        var formData = new FormData($("#upload_idnumber")[0]);
        var url;
        $.ajax({
            url: '/Manage/uploadAjaxFile.do',
            type: 'POST',
            data: formData,
            async: false,
            cache: false,
            contentType: false,
            processData: false,
            success: function (res) {
                if (res.success) {
                    url = res.message;
                    var img = url.split("_");
                    $("#hiddenID").val("tianxia_" + img[1]);
                }
            },
            error: function (res) {
                alert("上传失败" + res.message);
            }
        });
        $("#uploaded_img_idnumber").attr("src", url);
    });
    //上传营业执照
    $("#upload_license_button").click(function () {
        var formData = new FormData($("#upload_license")[0]);
        var url;
        $.ajax({
            url: '/Manage/uploadAjaxFile.do',
            type: 'POST',
            data: formData,
            async: false,
            cache: false,
            contentType: false,
            processData: false,
            success: function (res) {
                if (res.success) {
                    url = res.message;
                    var img = url.split("_");
                    $("#hiddenLicense").val("tianxia_" + img[1])
                }
            },
            error: function (res) {
                alert("上传失败" + res.message);
            }
        });
        $("#uploaded_license_img").attr("src", url);
    });
});
function getCategoryByParent() {

    $.ajax({
        type: "get",
        url: "/Manage/getCategoryByParent.do",
        data: {id: $.trim($("#cate_par").val())},
        timeout: 1000,
        dataType: "json",
        cache: false,
        success: function (data) {
            var strHtml = "<option value=\"0\" selected>选择类别</option>";
            if (data.length > 1) {
                for (var i = 0; i < data.length; i++) {
                    strHtml += "<option value='" + data[i].id + "'>" + data[i].name + "</option>";
                }
            }
            $("#cate_child").html(strHtml);
        }
    });
}
//验证必须是汉字
function isChinaName(name) {
    if (!(/^[\u4E00-\u9FA5]{1,6}$/.test(name))) {
        return false;
    }
    return true;
}
//验证手机号
function isMobile(mobile) {
    if (!(/^1[3|4|5|7|8][0-9]\d{8}$/.test(mobile))) {
        return false;
    }
    return true;
}
// 验证身份证 
function isCardNo(card) {
    var pattern = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    if (!pattern.test(card)) {
        return false;
    }
    return true;
}
// 验证邮箱 
function isEmail(card) {
    var pattern = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
    if (!pattern.test(card)) {
        return false;
    }
    return true;
}


